package com.gxa.meiye.admin.platform.controller;

import com.gxa.meiye.admin.platform.pojo.vo.*;
import com.gxa.meiye.admin.platform.service.RoleService;
import com.gxa.meiye.core.common.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@Api
@RestController
@RequestMapping("/platform/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperation("角色列表")
    @GetMapping("/list")
    public Map roleList(){
        List<RoleVo> roleVos = roleService.roleList();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(roleVos);
        jsonResult.setMsg("查询成功");
        return jsonResult.getMap();
    }

    @ApiOperation("根据名字查询角色")
    @PostMapping("/selectRoles")
    public Map selectUsers(String select){
        List<RoleVo> roleVos = roleService.selectRoles(select);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(roleVos);
        return jsonResult.getMap();
    }


    @ApiOperation("修改角色")
    @PostMapping("/updateRole")
    public Map updateRole(@RequestBody RoleVo roleVo){
        roleService.updateRole(roleVo);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");
        return jsonResult.getMap();
    }

    @ApiOperation("删除角色")
    @PostMapping("/del")
    public Map delRole(Integer id){
        Integer integer = roleService.delRole(id);
        JsonResult jsonResult = new JsonResult();
        if(integer>0) {
            jsonResult.setCode("200");
            jsonResult.setMsg("删除成功");
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("删除失败");
        }
        return jsonResult.getMap();
    }

    @ApiOperation("新增角色")
    @PostMapping("/addRole")
    public Map addRole(@RequestBody RoleVo roleVo){
        Integer integer = roleService.addRole(roleVo);
        JsonResult jsonResult = new JsonResult();
        if(integer>0) {
            jsonResult.setCode("200");
            jsonResult.setMsg("新增成功");
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("新增失败");
        }
        return jsonResult.getMap();
    }

    @ApiOperation("权限列表")
    @PostMapping("/permList")
    public Map permList(Integer id){
        Map<String, List<PermVo>> map = roleService.selectPerm(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("0");
        jsonResult.setData(map);
        return jsonResult.getMap();
    }

    @ApiOperation("设置权限")
    @PostMapping("/setPerm")
    public Map setPerm(@RequestBody SetPermVo setPermVo){
        roleService.setPerm(setPermVo);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("0");
        return jsonResult.getMap();
    }
}
